package cn.cvs.service.impl;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Override
    public SysUser queryUser(String account, String password) {
        SysUser sysUser = sysUserMapper.queryOne(account, password);
        return sysUser;
    }

    @Override
    public List<SysUser> querySysUsers(String realName, Integer roleId, Integer pageNo, Integer pageSize) {
        List<SysUser> sysUsers = sysUserMapper.queryByPage(realName, roleId, (pageNo - 1) *pageSize, pageSize);
        return sysUsers;
    }

    @Override
    public int queryCount(String realName, Integer roleId) {
        int count = sysUserMapper.queryCount(realName, roleId);
        return count;
    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int addUser(SysUser sysUser) {
        int add = sysUserMapper.addAll(sysUser);
        return add;
    }

    @Override//第一次查询的时候将数据保存到缓存
    public SysUser queryById(Integer id) {
        Object user = redisTemplate.opsForValue().get("queryById_" + id);
        if (user!=null){
            return (SysUser)user;
        }
        SysUser sysUser = sysUserMapper.queryById(id);
        if (sysUser!=null){
            redisTemplate.opsForValue().set("queryById_"+id,sysUser);
        }
        return sysUser;
    }

    @Override//修改的时候要清除缓存
    public int updateUser(SysUser sysUser) {
        int update = sysUserMapper.update(sysUser);
        Object user = redisTemplate.opsForValue().get("queryById_" + sysUser.getId());
        if (user!=null){
            //清除缓存
            redisTemplate.delete("queryById_"+sysUser.getId());
        }
        return update;
    }

    @Override
    public int queryUserExist(String account) {
        int i = sysUserMapper.queryUserExist(account);
        return i;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override//删除的时候要清除缓存
    public int deleteUser(Integer id, HttpServletRequest request) {
        //删除图片
        SysUser sysUser = sysUserMapper.queryById(id);
        //删除数据库中的记录
        int i = sysUserMapper.deleteById(id);
        //身份照
        if (!StringUtils.isEmpty(sysUser.getIdPicPath())){
            //获得身份照在服务器的物理地址
            String idPicPath=
            request.getSession().getServletContext().getRealPath(sysUser.getIdPicPath());
            File idPic = new File(idPicPath);
            if (idPic.exists()){
                idPic.delete();
            }
        }
        //工作照
        if (!StringUtils.isEmpty(sysUser.getWorkPicPath())){
            //获得工作照的物理地址
            String workPicPath=
                    request.getSession().getServletContext().getRealPath(sysUser.getWorkPicPath());
            File workPic = new File(workPicPath);
            if (workPic.exists()) {
                workPic.delete();
            }
        }
        Object user = redisTemplate.opsForValue().get("queryById_" + id);
        if (user!=null){
            //清除缓存
            redisTemplate.delete("queryById_"+id);
        }
        return i;
    }


}
